package com.example.admin.test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String apkPath = Environment.getExternalStorageDirectory() + "/Download/merchant-debug.apk";
                onSilentInstall(apkPath, v);
            }
        });

        findViewById(R.id.unInstall).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSlientUnInstall(v, "com.hgtech.ssm");
            }
        });

    }


    public void onSlientUnInstall(View view, final String packageName) {
        if (!isRoot()) {
            Toast.makeText(this, "没有ROOT权限，不能使用静默卸载", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(packageName)) {
            Toast.makeText(this, "包名为空", Toast.LENGTH_SHORT).show();
        }
        final Button button = (Button) view;
        button.setText("卸载中");
        new Thread(new Runnable() {
            @Override
            public void run() {
                SilentUnInstall unInstall = new SilentUnInstall();
                final boolean result = unInstall.unInstallApk(packageName);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result)
                            Toast.makeText(getApplicationContext(), "卸载成功", Toast.LENGTH_SHORT).show();
                        else {
                            Toast.makeText(getApplicationContext(), "卸载失败", Toast.LENGTH_SHORT).show();
                        }
                        button.setText("卸载");
                    }
                });

            }
        }).start();
    }


    public void onSilentInstall(final String apkPath, View view) {
        if (!isRoot()) {
            Toast.makeText(this, "没有ROOT权限，不能使用秒装", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(apkPath)) {
            Toast.makeText(this, "请选择安装包！", Toast.LENGTH_SHORT).show();
            return;
        }
        final Button button = (Button) view;
        button.setText("安装中");
        new Thread(new Runnable() {
            @Override
            public void run() {
                SilentInstall installHelper = new SilentInstall();
                final boolean result = installHelper.install(apkPath);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result) {
                            Toast.makeText(MainActivity.this, "安装成功！", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "安装失败！", Toast.LENGTH_SHORT).show();
                        }
                        button.setText("秒装");
                    }
                });
            }
        }).start();
    }


    /**
     * 判断手机是否拥有Root权限。
     *
     * @return 有root权限返回true，否则返回false。
     */
    public boolean isRoot() {
        boolean bool = false;
        try {
            bool = new File("/system/bin/su").exists() || new File("/system/xbin/su").exists();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bool;
    }

}
